package com.gaohe.handler;

import com.gaohe.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

//异常捕获器
@Slf4j
@RestControllerAdvice
public class GlobalHandler {
    //捕获指定异常
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R idx_username(SQLIntegrityConstraintViolationException e){
       log.info("用户行为异常：{}",e.getMessage());
       e.printStackTrace();
       String msg=e.getMessage();
        String[] s = msg.split(" ");
        if(s.length>=3){
            msg=s[2]+"用户名已存在";
        }
        return R.error(msg);

    }
}
